/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import entities.Image;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class ImageDB {
    private String url;
    private String user;
    private String password;

    public ImageDB(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }
    
    public List<Image> showImage(){
        List<Image> list = new ArrayList<>();
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call showImage}");
            ResultSet rs = cstmt.executeQuery();
            while(rs.next()){
                int id = rs.getInt(1);
                String imagename = rs.getString(2);
                String dayshooting = rs.getString(3);
                String username = rs.getString(4);
                String albumname = rs.getString(5);
                String turnlike = rs.getString(6);
                String introduction = rs.getString(7);
                
                Image img = new Image(id, imagename, dayshooting, username, albumname, turnlike, introduction);
                list.add(img);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ImageDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
    
    public boolean insertImage(Image img){
        try {
            Connection conn = ConnectionUtil.getConnection(url, user, password);
            CallableStatement cstmt = conn.prepareCall("{call insertImage(?, ?, ?, ?, ?, ?)}");
            cstmt.setString(1, img.getImagename());
            cstmt.setString(2, img.getDayshooting());
            cstmt.setString(3, img.getUsername());
            cstmt.setString(4, img.getAlbumname());
            cstmt.setString(5, img.getTurnlike());
            cstmt.setString(6, img.getIntroduction());
            int rows = cstmt.executeUpdate();
            conn.close();
            return rows > 0 ;
        } catch (SQLException ex) {
            Logger.getLogger(ImageDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
